package com.kaifamiao.thread.d0929;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class Server {

    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8889);

            while(true){
                // 建立连接
                Socket accept = server.accept();

                new Thread(()->{
                    System.out.print(accept.getInetAddress().getHostAddress());

                    // 获取输入流
                    InputStream inputStream = null;
                    try {
                        inputStream = accept.getInputStream();
                        String fileName = UUID.randomUUID().toString();
                        FileOutputStream fos = new FileOutputStream("d:/" + fileName + ".jpg");
                        // 接收消息
                        inputStream.transferTo(fos);

                        fos.close();
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();


            }




        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
